#!/bin/bash

# it should be fixed
ENABLE_STDOUT_LOGGING="enableStdOutLogging"
GENERAL_CONFIG_DIR="/etc/indy"
GENERAL_DATA_DIR="/var/lib/indy"
GENERAL_LOG_DIR="/var/log/indy"

INSTALL_DIR='/usr/local/lib/python3.5/dist-packages'

NOFILES_SOFT_LIMIT=65536
NOFILES_HARD_LIMIT=131072

CLIENT_CONNECTIONS_LIMIT=500


# create general indy config folder if does not exist
mkdir -p $GENERAL_CONFIG_DIR
# create general indy data folder if does not exist
mkdir -p $GENERAL_DATA_DIR
# create general indy log folder if does not exist
mkdir -p $GENERAL_LOG_DIR

# create indy node config if does not exist
if [ ! -f $GENERAL_CONFIG_DIR/indy_config.py ]; then
    cp $INSTALL_DIR/indy_node/general_config/indy_config.py $GENERAL_CONFIG_DIR
else
    if [ -z "$(grep $ENABLE_STDOUT_LOGGING $GENERAL_CONFIG_DIR/indy_config.py)" ]; then
        printf "\n%s\n%s\n" "# Disable stdout logging" "$ENABLE_STDOUT_LOGGING = False" >> $GENERAL_CONFIG_DIR/indy_config.py
    fi
fi

chmod -R ug+rwx $GENERAL_CONFIG_DIR
chmod -R ug+rwx $GENERAL_DATA_DIR
chmod -R ug+rwx $GENERAL_LOG_DIR


# init_indy_node script
cat <<EOF > /usr/local/bin/init_indy_node
#!/bin/bash

if [ \$# -lt 5 ]; then
    echo ""
    echo "Usage: \$0 name ip port client_ip client_port [seed]";
    echo "  name        - node name";
    echo "  ip          - node IP";
    echo "  port        - node port";
    echo "  client_ip   - node client IP";
    echo "  client_port - node client port";
    echo "  seed        - node seed";
    echo ""
    exit 1;
fi

echo "NODE_NAME=\$1" > $GENERAL_CONFIG_DIR/indy.env
echo "NODE_IP=\$2" >> $GENERAL_CONFIG_DIR/indy.env
echo "NODE_PORT=\$3" >> $GENERAL_CONFIG_DIR/indy.env
echo "NODE_CLIENT_IP=\$4" >> $GENERAL_CONFIG_DIR/indy.env
echo "NODE_CLIENT_PORT=\$5" >> $GENERAL_CONFIG_DIR/indy.env
echo "CLIENT_CONNECTIONS_LIMIT=$CLIENT_CONNECTIONS_LIMIT" >> $GENERAL_CONFIG_DIR/indy.env

if [ -z \$6 ]; then
    init_indy_keys --name \$1
else
    init_indy_keys --name \$1 --seed \$6
fi
EOF

chmod +x /usr/local/bin/init_indy_node

# add systemd script
cat <<EOF > /etc/systemd/system/indy-node.service
[Unit]
Description=Indy Node
Requires=indy-node-control.service

[Service]
EnvironmentFile=$GENERAL_CONFIG_DIR/indy.env
ExecStart=/usr/bin/env python3 -O /usr/local/bin/start_indy_node \${NODE_NAME} \${NODE_IP} \${NODE_PORT} \${NODE_CLIENT_IP} \${NODE_CLIENT_PORT}
User=indy
Group=indy
Restart=on-failure
RestartSec=10
StartLimitBurst=10
StartLimitInterval=200
TimeoutSec=300
LimitNOFILE=$NOFILES_SOFT_LIMIT:$NOFILES_HARD_LIMIT

[Install]
WantedBy=multi-user.target
EOF


cat <<EOF > /etc/systemd/system/indy-node-control.service
[Unit]
Description=Service for upgrade of existing Indy Node and other operations
#Requires=indy.service
#After=indy.service
After=network.target

[Service]
Type=simple
EnvironmentFile=$GENERAL_CONFIG_DIR/node_control.conf
ExecStart=/usr/bin/env python3 -O /usr/local/bin/start_node_control_tool \$TEST_MODE --hold-ext \${HOLD_EXT}
Restart=on-failure
RestartSec=10
StartLimitBurst=10
StartLimitInterval=200
TimeoutSec=300

[Install]
WantedBy=multi-user.target
EOF

# add supervisord script
cat <<EOF > /etc/supervisor/indy-node.conf
[supervisord]
nodaemon=true
logfile=/var/log/indy/supervisord.log
logfile_maxbytes=10MB
loglevel=critical

[supervisorctl]
serverurl=http://127.0.0.1:9001

[inet_http_server]
port = 127.0.0.1:9001

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[program:indy-node]
command=sh -ac '. $GENERAL_CONFIG_DIR/indy.env;/usr/bin/env python3 -O /usr/local/bin/start_indy_node \${NODE_NAME} \${NODE_IP} \${NODE_PORT} \${NODE_CLIENT_IP} \${NODE_CLIENT_PORT}'
user=indy
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=true
startsecs=15
startretries=6
stopasgroup=true
killasgroup=true

[program:indy-node-control]
command=sh -ac '. $GENERAL_CONFIG_DIR/node_control.conf;/usr/bin/env python3 -O /usr/local/bin/start_node_control_tool \${TEST_MODE} --hold-ext "\${HOLD_EXT}"'
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=true
startsecs=15
startretries=6
stopasgroup=true
killasgroup=true
EOF

HOLD_EXT_ADDED=$(grep HOLD_EXT /etc/indy/node_control.conf)
if [ ! -f $GENERAL_CONFIG_DIR/node_control.conf ] || [ -z "${HOLD_EXT_ADDED}" ]; then
    cat <<EOF > $GENERAL_CONFIG_DIR/node_control.conf
# Uncomment this to run agent in test mode:
#TEST_MODE=--test

TEST_MODE=
HOLD_EXT=
EOF
fi
sed -ie '/HOLD_EXT/{ s/\\//g}' $GENERAL_CONFIG_DIR/node_control.conf

mv /usr/local/bin/upgrade_indy_node_ubuntu1604.sh /usr/local/bin/upgrade_indy_node
mv /usr/local/bin/upgrade_indy_node_ubuntu1604_test.sh /usr/local/bin/upgrade_indy_node_test
mv /usr/local/bin/restart_indy_node_ubuntu1604.sh /usr/local/bin/restart_indy_node
mv /usr/local/bin/restart_sovrin_node_ubuntu1604.sh /usr/local/bin/restart_sovrin_node
mv /usr/local/bin/complete_rebranding_upgrade_ubuntu1604.sh /usr/local/bin/complete_rebranding_upgrade

chmod +x /usr/local/bin/upgrade_indy_node
chmod +x /usr/local/bin/upgrade_indy_node_test
chmod +x /usr/local/bin/restart_indy_node
chmod +x /usr/local/bin/restart_sovrin_node
chmod +x /usr/local/bin/complete_rebranding_upgrade

rm -f /usr/local/bin/delete_indy_node.bat /usr/local/bin/upgrade_indy_node_test.bat \
    /usr/local/bin/restart_indy_node.bat /usr/local/bin/install_nssm.bat /usr/local/bin/upgrade_indy_node.bat \
    /usr/local/bin/install_indy_node.bat /usr/local/bin/restart_upgrade_agent.bat

chown -R indy:indy $GENERAL_CONFIG_DIR
chown -R indy:indy $GENERAL_DATA_DIR
chown -R indy:indy $GENERAL_LOG_DIR
chmod -R ug+rw $GENERAL_CONFIG_DIR
chmod -R ug+rw $GENERAL_DATA_DIR
chmod -R ug+rw $GENERAL_LOG_DIR

# Automatically added from template:
if which py3compile >/dev/null 2>&1; then
	py3compile -O -p indy-node $INSTALL_DIR
fi

# End automatically added section
